/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.coopeud.persistencia;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 *
 * @author Usuario
 */
public class GestorConexion {
    
    private String resource = "config/mybatis-config.xml";
    private InputStream inputStream;
    private SqlSessionFactory sqlSessionFactory;
    private String username;
    private String password;

    
    
    private static GestorConexion instanciaGestorConexion;
    
    private GestorConexion(){
        
    }
    
    public static GestorConexion getInstanciaGestorConexion(){
        
        if(instanciaGestorConexion == null){
            
           instanciaGestorConexion = new GestorConexion();
        }
        
        return instanciaGestorConexion;
    }

    public SqlSessionFactory getSqlSessionFactory(){
        
        try {
            this.inputStream = Resources.getResourceAsStream(resource);
                       
            Properties propierties = new Properties();
            propierties.setProperty("username",this.username);
            propierties.setProperty("password",this.password);
            
            //propierties.setProperty("username","coopeud");
            //propierties.setProperty("password","coopeud");
            
            this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, propierties);
           
        } catch (IOException ex) {
            Logger.getLogger(GestorConexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return this.sqlSessionFactory;
    }
    
    public void setUsername(String username) {
        this.username = username;
        System.out.println("setUsername: "+username);
    }

    public void setPassword(String password) {
        this.password = password;
        System.out.println("setPassword: "+password);
    }
    
    
        
}
